@echo off
REM B站弹幕机 JMeter 自动化测试脚本 (Windows版本)
REM 作者: AI Assistant
REM 版本: 1.0

setlocal enabledelayedexpansion

REM 配置变量
set PROJECT_NAME=B站弹幕机
set PROJECT_PORT=23333
set PROJECT_URL=http://localhost:%PROJECT_PORT%
set JMETER_PLAN=jmeter_test_plan.jmx
set TEST_RESULTS=test_results.jtl
set TEST_REPORT_DIR=test_report
set LOG_FILE=test_execution.log

REM 颜色定义 (Windows CMD不支持ANSI颜色，使用echo)
echo [INFO] B站弹幕机 JMeter 自动化测试脚本 (Windows版本)
echo [INFO] 测试时间: %date% %time%

REM 创建日志文件
echo [INFO] 开始执行 %PROJECT_NAME% JMeter 自动化测试 > %LOG_FILE%
echo [INFO] 测试时间: %date% %time% >> %LOG_FILE%

REM 检查依赖
echo [INFO] 检查系统依赖...
echo [INFO] 检查系统依赖... >> %LOG_FILE%

REM 检查Java
java -version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Java未安装，请先安装Java 8或更高版本
    echo [ERROR] Java未安装，请先安装Java 8或更高版本 >> %LOG_FILE%
    exit /b 1
)

REM 检查JMeter
jmeter -v >nul 2>&1
if errorlevel 1 (
    echo [ERROR] JMeter未安装，请先安装Apache JMeter
    echo [ERROR] JMeter未安装，请先安装Apache JMeter >> %LOG_FILE%
    exit /b 1
)

REM 检查Gradle wrapper
if not exist "gradlew.bat" (
    echo [ERROR] Gradle wrapper不存在，请确保在项目根目录执行
    echo [ERROR] Gradle wrapper不存在，请确保在项目根目录执行 >> %LOG_FILE%
    exit /b 1
)

echo [SUCCESS] 所有依赖检查通过
echo [SUCCESS] 所有依赖检查通过 >> %LOG_FILE%

REM 检查端口占用
echo [INFO] 检查端口 %PROJECT_PORT% 占用情况...
echo [INFO] 检查端口 %PROJECT_PORT% 占用情况... >> %LOG_FILE%

netstat -ano | findstr ":%PROJECT_PORT%" >nul
if not errorlevel 1 (
    echo [WARNING] 端口 %PROJECT_PORT% 已被占用，尝试停止占用进程...
    echo [WARNING] 端口 %PROJECT_PORT% 已被占用，尝试停止占用进程... >> %LOG_FILE%
    
    for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":%PROJECT_PORT%"') do (
        taskkill /f /pid %%a >nul 2>&1
    )
    timeout /t 2 /nobreak >nul
)

echo [SUCCESS] 端口 %PROJECT_PORT% 可用
echo [SUCCESS] 端口 %PROJECT_PORT% 可用 >> %LOG_FILE%

REM 启动项目
echo [INFO] 启动 %PROJECT_NAME% 项目...
echo [INFO] 启动 %PROJECT_NAME% 项目... >> %LOG_FILE%

REM 构建项目
echo [INFO] 构建项目...
echo [INFO] 构建项目... >> %LOG_FILE%
call gradlew.bat build -x test
if errorlevel 1 (
    echo [ERROR] 项目构建失败
    echo [ERROR] 项目构建失败 >> %LOG_FILE%
    exit /b 1
)

REM 启动项目
echo [INFO] 启动项目...
echo [INFO] 启动项目... >> %LOG_FILE%
start /b java -jar build\libs\danmuji-2.7.0.2.jar > project.log 2>&1

REM 等待项目启动
echo [INFO] 等待项目启动...
echo [INFO] 等待项目启动... >> %LOG_FILE%

set /a attempts=0
:wait_loop
set /a attempts+=1
if %attempts% gtr 30 (
    echo [ERROR] 项目启动超时
    echo [ERROR] 项目启动超时 >> %LOG_FILE%
    exit /b 1
)

curl -s %PROJECT_URL% >nul 2>&1
if errorlevel 1 (
    timeout /t 1 /nobreak >nul
    goto wait_loop
)

echo [SUCCESS] 项目启动成功
echo [SUCCESS] 项目启动成功 >> %LOG_FILE%

REM 执行JMeter测试
echo [INFO] 开始执行JMeter测试...
echo [INFO] 开始执行JMeter测试... >> %LOG_FILE%

REM 检查JMeter测试计划文件
if not exist "%JMETER_PLAN%" (
    echo [ERROR] JMeter测试计划文件不存在: %JMETER_PLAN%
    echo [ERROR] JMeter测试计划文件不存在: %JMETER_PLAN% >> %LOG_FILE%
    exit /b 1
)

REM 执行测试
echo [INFO] 执行测试计划: %JMETER_PLAN%
echo [INFO] 执行测试计划: %JMETER_PLAN% >> %LOG_FILE%

jmeter -n -t %JMETER_PLAN% -l %TEST_RESULTS% -e -o %TEST_REPORT_DIR%
if errorlevel 1 (
    echo [ERROR] JMeter测试执行失败
    echo [ERROR] JMeter测试执行失败 >> %LOG_FILE%
    exit /b 1
)

echo [SUCCESS] JMeter测试执行完成
echo [INFO] 测试结果文件: %TEST_RESULTS%
echo [INFO] 测试报告目录: %TEST_REPORT_DIR%
echo [SUCCESS] JMeter测试执行完成 >> %LOG_FILE%
echo [INFO] 测试结果文件: %TEST_RESULTS% >> %LOG_FILE%
echo [INFO] 测试报告目录: %TEST_REPORT_DIR% >> %LOG_FILE%

REM 分析测试结果
echo [INFO] 分析测试结果...
echo [INFO] 分析测试结果... >> %LOG_FILE%

if not exist "%TEST_RESULTS%" (
    echo [WARNING] 测试结果文件不存在，跳过分析
    echo [WARNING] 测试结果文件不存在，跳过分析 >> %LOG_FILE%
    goto cleanup
)

REM 尝试使用简化版分析脚本
if exist "jmeter_test_scripts\simple_analyze.py" (
    echo [INFO] 使用简化版分析脚本...
    echo [INFO] 使用简化版分析脚本... >> %LOG_FILE%
    python jmeter_test_scripts\simple_analyze.py
    if errorlevel 1 (
        echo [WARNING] 简化版分析脚本执行失败，使用基础统计
        echo [WARNING] 简化版分析脚本执行失败，使用基础统计 >> %LOG_FILE%
        goto basic_stats
    )
) else (
    echo [WARNING] 简化版分析脚本不存在，使用基础统计
    echo [WARNING] 简化版分析脚本不存在，使用基础统计 >> %LOG_FILE%
    goto basic_stats
)

goto cleanup

:basic_stats
REM 基础统计
echo [INFO] 执行基础统计...
echo [INFO] 执行基础统计... >> %LOG_FILE%

REM 统计基本信息
for /f %%i in ('type %TEST_RESULTS% ^| find /c /v ""') do set TOTAL_REQUESTS=%%i
for /f %%i in ('findstr /c:"true" %TEST_RESULTS% ^| find /c /v ""') do set SUCCESS_REQUESTS=%%i
for /f %%i in ('findstr /c:"false" %TEST_RESULTS% ^| find /c /v ""') do set FAILED_REQUESTS=%%i

if "%SUCCESS_REQUESTS%"=="" set SUCCESS_REQUESTS=0
if "%FAILED_REQUESTS%"=="" set FAILED_REQUESTS=0

echo [INFO] 测试统计:
echo [INFO]   总请求数: %TOTAL_REQUESTS%
echo [INFO]   成功请求数: %SUCCESS_REQUESTS%
echo [INFO]   失败请求数: %FAILED_REQUESTS%
echo [INFO] 测试统计: >> %LOG_FILE%
echo [INFO]   总请求数: %TOTAL_REQUESTS% >> %LOG_FILE%
echo [INFO]   成功请求数: %SUCCESS_REQUESTS% >> %LOG_FILE%
echo [INFO]   失败请求数: %FAILED_REQUESTS% >> %LOG_FILE%

REM 计算成功率
if %TOTAL_REQUESTS% gtr 0 (
    set /a SUCCESS_RATE=%SUCCESS_REQUESTS% * 100 / %TOTAL_REQUESTS%
    echo [INFO]   成功率: %SUCCESS_RATE%%%
    echo [INFO]   成功率: %SUCCESS_RATE%%% >> %LOG_FILE%
)

REM 生成简单报告
echo B站弹幕机 JMeter 测试报告 > test_summary.txt
echo ======================== >> test_summary.txt
echo. >> test_summary.txt
echo 测试时间: %date% %time% >> test_summary.txt
echo 测试环境: %COMPUTERNAME% >> test_summary.txt
echo 项目版本: 2.7.0.2 >> test_summary.txt
echo. >> test_summary.txt
echo 测试统计: >> test_summary.txt
echo - 总请求数: %TOTAL_REQUESTS% >> test_summary.txt
echo - 成功请求数: %SUCCESS_REQUESTS% >> test_summary.txt
echo - 失败请求数: %FAILED_REQUESTS% >> test_summary.txt
if %TOTAL_REQUESTS% gtr 0 (
    echo - 成功率: %SUCCESS_RATE%%% >> test_summary.txt
)
echo. >> test_summary.txt
echo 详细报告请查看: %TEST_REPORT_DIR%\index.html >> test_summary.txt

echo [SUCCESS] 测试分析完成，报告已生成: test_summary.txt
echo [SUCCESS] 测试分析完成，报告已生成: test_summary.txt >> %LOG_FILE%

:cleanup
REM 清理资源
echo [INFO] 清理测试资源...
echo [INFO] 清理测试资源... >> %LOG_FILE%

REM 停止项目进程
for /f "tokens=2" %%a in ('tasklist /fi "imagename eq java.exe" /fo csv ^| findstr "danmuji"') do (
    taskkill /f /pid %%a >nul 2>&1
)

REM 清理临时文件
if exist project.log del project.log

echo [SUCCESS] 资源清理完成
echo [SUCCESS] 资源清理完成 >> %LOG_FILE%

echo.
echo [SUCCESS] 所有测试执行完成！
echo [INFO] 查看详细报告: %TEST_REPORT_DIR%\index.html
echo [INFO] 查看测试日志: %LOG_FILE%
echo [SUCCESS] 所有测试执行完成！ >> %LOG_FILE%
echo [INFO] 查看详细报告: %TEST_REPORT_DIR%\index.html >> %LOG_FILE%
echo [INFO] 查看测试日志: %LOG_FILE% >> %LOG_FILE%

pause 